package Question3_2;

import java.util.Random;
import java.util.Stack;

@SuppressWarnings("serial")
public class Stackwithmin extends Stack<Integer> {
	Stack<Integer> min;

	public Stackwithmin() {
		min = new Stack<Integer>();
	}

	public void push(int item) {

		if (item <= min()) {
			min.push(item);
		}

		super.push(item);
	}

	public Integer pop() {
		int item = super.pop();
		if (item == min()) {
			min.pop();
		}
		return item;
	}

	public int min() {
		if (isEmpty()) {
			return Integer.MAX_VALUE;
		} else {
			return min.peek();
		}
	}
	
	public static void main(String[] args) { 
        Stackwithmin stack2 = new Stackwithmin(); 
        StackWithMin2 stack = new StackWithMin2(); 
        Random r = new Random();
        for (int i = 0; i < 15; i++) { 
            int value = r.nextInt(100); 
            stack.push(value); 
            stack2.push(value); 
            System.out.print(value + ", "); 
        } 
        System.out.println('\n'); 
        for (int i = 0; i < 15; i++) { 
            System.out.println("Popped " + stack.pop().data + ", " + stack2.pop()); 
            System.out.println("New min is " + stack.min() + ", " + stack2.min()); 
        } 
    } 
}
